package com.ruoyi.develop.goodsReplace.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.constants.BusinessConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsReplace.domain.GoodsReplace;
import com.ruoyi.develop.goodsReplace.service.IGoodsReplaceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存编码替换Controller
 *
 * @author lmm
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/goodsReplace/goodsReplace")
public class GoodsReplaceController extends BaseController {
    @Autowired
    private IGoodsReplaceService goodsReplaceService;

    /**
     * 查询库存编码替换列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReplace:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsReplace entity) {
        startPage();
        List<GoodsReplace> list = goodsReplaceService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出库存编码替换列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReplace:export')")
    @Log(title = "库存编码替换", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsReplace entity) {
        List<GoodsReplace> list = goodsReplaceService.selectList(entity);
        ExcelUtil<GoodsReplace> util = new ExcelUtil<GoodsReplace>(GoodsReplace.class);
        util.exportExcel(response, list, "库存编码替换数据");
    }

    /**
     * 获取库存编码替换详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReplace:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsReplaceService.selectById(id));
    }


    /**
     * 开始替换操作
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReplace:query')")
    @GetMapping(value = "/replace/{id}")
    public AjaxResult replace(@PathVariable("id") String id) {
        GoodsReplace entity = goodsReplaceService.selectById(id);
        // 先判断替换和被替换的 是否是同一个
        if (entity.getGoodsId().equals(entity.getReplacedGoodsId())) {
            return AjaxResult.error("替换和被替换的产品不能是同一个！");
        }

//        try {
            goodsReplaceService.replace(entity);
            entity.setStatus(BusinessConstants.GOODS_REPLACE_STATUS_SUCCESS);
            goodsReplaceService.update(entity);
//        }
//        catch (Exception e) {
//            entity.setStatus(BusinessConstants.GOODS_REPLACE_STATUS_FAIL);
//            goodsReplaceService.update(entity);
//            return AjaxResult.error("替换失败！");
//        }
        return AjaxResult.success("操作成功！");
    }


    /**
     * 新增库存编码替换
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReplace:add')")
    @Log(title = "库存编码替换", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsReplace entity) {
        return toAjax(goodsReplaceService.insert(entity));
    }

    /**
     * 修改库存编码替换
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReplace:edit')")
    @Log(title = "库存编码替换", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsReplace entity) {
        return toAjax(goodsReplaceService.update(entity));
    }

    /**
     * 删除库存编码替换
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReplace:remove')")
    @Log(title = "库存编码替换", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsReplaceService.deleteByIds(ids));
    }
}
